package com.hardware.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hardware.common.BaseContext;
import com.hardware.common.R;
import com.hardware.common.Result;
import com.hardware.entity.User;
import com.hardware.service.UserService;
import com.hardware.utils.SMSUtils;
import com.hardware.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * 用户管理
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送短信验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user){
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        if(user.getPhone() != null){
            SMSUtils.sendMessage("若久","SMS_274930103",user.getPhone(),code);
            log.info("code is {}",code);
            //将验证码存入redis中
            redisTemplate.opsForValue().set(user.getPhone(),code,5, TimeUnit.MINUTES);
            return R.success("发送成功");
        }
        return R.error("发送失败");
    }

    /**
     * 用户登录
     * @param session
     * @param map
     * @return
     */
    @PostMapping("/login")
    public R<User> login(HttpSession session, @RequestBody Map map){

        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        //redis中获取验证码
        Object codeInSession = redisTemplate.opsForValue().get(phone);
//        log.info(codeInSession.toString());
        if(codeInSession != null && codeInSession.equals(code)){
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(phone != null,User::getPhone,phone);
            User user = userService.getOne(lqw);
            if(user == null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                user.setName("frd_"+ RandomUtil.randomNumbers(5));
                userService.save(user);
            }
            redisTemplate.delete(phone);

            session.setAttribute("user",user.getId());
            return R.success(user);
        }
        return R.error("验证码有误,请重试");
    }

    /**
     * 用户退出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        Long currentId = BaseContext.getCurrentId();
        session.removeAttribute("user");
        return R.success("退出成功");
    }


    @PostMapping("/code")
    public Result code(@RequestParam String phone){

        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        if(phone != null){
//            SMSUtils.sendMessage("若久","SMS_274930103",phone,code);
            log.info("code is {}",code);
            //将验证码存入redis中
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return Result.ok("发送成功");
        }
        return Result.fail("发送失败");
    }

   /* @PostMapping("/login")
    public Result login2(HttpSession session, @RequestBody Map map){
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        //redis中获取验证码
        Object codeInSession = redisTemplate.opsForValue().get(phone);
        if(codeInSession != null && codeInSession.equals(code)){
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(phone != null,User::getPhone,phone);
            User user = userService.getOne(lqw);
            if(user == null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                user.setName("frd_"+ RandomUtil.randomNumbers(5));
                userService.save(user);
            }
            redisTemplate.delete(phone);

            session.setAttribute("user",user.getId());
            return Result.ok("123");
        }
        return Result.fail("验证码有误,请重试");
    }*/



}
